//
//  TreeMapViewController.m
//  iStock Global
//
//  Created by 英傑 翁 on 11/12/8.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "TreeMapViewController.h"

@implementation TreeMapViewController

 //標記:增加Treemap 至 viewcontroller 資料宣告
@synthesize fruits;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

 
// Implement loadView to create a view hierarchy programmatically, without using a nib.
- (void)loadView
{
    UIView *mainview;
	mainview=[[UIView alloc]initWithFrame:[UIScreen mainScreen].applicationFrame];
	self.view=mainview;    
    [mainview release];
    
    //標記:增加Treemap 至 viewcontroller 實體化
    mytreemap = [[TreemapView alloc] init ];
    mytreemap.frame= CGRectMake(0.0f, 0.0f, 320.0f, 350.0f) ;
    
    mytreemap.delegate=self;
    mytreemap.dataSource=self;
    
    [self.view addSubview:mytreemap];     
    
}
-(void)initTitle:(NSString *)bartitle{
	self.title=bartitle;
	 	
	NSLog(@"initTitle");
	
}
-(void)initImage:(UIImage*)image{
	self.tabBarItem.image= image ;
	NSLog(@"initImage");
}
-(void)get_navcontroller:(navcontroller *)nd{
	navdelegete=nd;
}

 
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
 
 
 
}
 

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

#pragma mark -
#pragma mark -TreemapView updateCell
//更新或是輸入每個cell的表面呈現資料
- (void)updateCell:(TreemapViewCell *)cell forIndex:(NSInteger)index {
	
    NSNumber *val = [[fruits objectAtIndex:index] valueForKey:@"value"];
	cell.textLabel.text = [[fruits objectAtIndex:index] valueForKey:@"name"];
	cell.valueLabel.text = [val stringValue];
    cell.backgroundColor = [UIColor colorWithHue:(float)index / (fruits.count + 3) saturation:1 brightness:0.75 alpha:1];
}

//標記:增加Treemap 至 viewcontroller 委託方法與資料源
#pragma mark -
#pragma mark TreemapView delegate
//觸碰到每個cell 的動作
- (void)treemapView:(TreemapView *)treemapView tapped:(NSInteger)index {
	/*
	 * change the value
	 */
	NSDictionary *dic = [fruits objectAtIndex:index];
	[dic setValue:[NSNumber numberWithInt:[[dic valueForKey:@"value"] intValue] + 300]
		   forKey:@"value"];
    
	/*
	 * resize rectangles with animation
	 */
	[UIView beginAnimations:@"reload" context:nil];
	[UIView setAnimationDuration:0.5];
    
    //標記:增加Treemap 至 viewcontroller 回復資料啟動    
	[mytreemap reloadData];
    
	[UIView commitAnimations];
    
	/*
	 * highlight the background
	 */
	[UIView beginAnimations:@"highlight" context:nil];
	[UIView setAnimationDuration:1.0];
    
    
	TreemapViewCell *cell = (TreemapViewCell *)[mytreemap.subviews objectAtIndex:index];
	UIColor *color = cell.backgroundColor;
	[cell setBackgroundColor:[UIColor whiteColor]];
	[cell setBackgroundColor:color];
    
	[UIView commitAnimations];
}

#pragma mark -
#pragma mark TreemapView data source

//輸入每個cell的大小資料 利用大小數據陣列畫出每個陣列
- (NSArray *)valuesForTreemapView:(TreemapView *)treemapView {
	
    if (!fruits) {
		NSBundle *bundle = [NSBundle mainBundle];
		NSString *plistPath = [bundle pathForResource:@"data" ofType:@"plist"];
		NSArray *array = [[[NSArray alloc] initWithContentsOfFile:plistPath] autorelease];
        
		self.fruits = [[[NSMutableArray alloc] initWithCapacity:array.count] autorelease];
		for (NSDictionary *dic in array) {
			NSMutableDictionary *mDic = [NSMutableDictionary dictionaryWithDictionary:dic];
			[fruits addObject:mDic];
		}
	}
    
	NSMutableArray *values = [NSMutableArray arrayWithCapacity:fruits.count];
	for (NSDictionary *dic in fruits) {
		[values addObject:[dic valueForKey:@"value"]];
	}
	return values;
}
//每個cell的初始化
- (TreemapViewCell *)treemapView:(TreemapView *)treemapView cellForIndex:(NSInteger)index forRect:(CGRect)rect {
	TreemapViewCell *cell = [[[TreemapViewCell alloc] initWithFrame:rect] autorelease];
	[self updateCell:cell forIndex:index];
	return cell;
}
//更新cell 外觀
- (void)treemapView:(TreemapView *)treemapView updateCell:(TreemapViewCell *)cell forIndex:(NSInteger)index forRect:(CGRect)rect {
	[self updateCell:cell forIndex:index];
}


@end
